<template>
  <view class="goods-item">
    <!-- 商品左边的图片 -->
    <view class="goods-item-left">
      <radio v-if="showRadio" :checked="goods.goods_state" color="#C00000" @click="radioClick(goods)" />
      <image :src="goods.goods_small_logo || defaultPic" class="goods-pic" />
    </view>
    <!-- 商品右侧的标题和价格 -->
    <view class="goods-item-right">
      <text class="goods-name">{{goods.goods_name}}</text>
      <view class="goods-info-box">
        <text class="goods-price">￥{{goods.goods_price | toFixed}}</text>  <!-- 这里的toFixed为自定义过淲器，非js的 toFixed -->
        <uni-number-box v-if="showNumBox" :min="1" v-model:value="goods.goods_count" :step="1" color="#333" background="#f5f5f5" :disabled="false" @change="numChangeHandler"></uni-number-box>
      </view>
    </view>
  </view>
</template>

<script>
  export default {
    name:"my-goods",
    // 定义过滤器（类似函数），在模板html标签或值中用 var | filterName 方式调用，var作为参数传给过淲器 filterName
    filters: {
      toFixed(num) {
        return Number(num).toFixed(2)
      }
    },
    props: {
      goods: {
        type: Object,
        default: {}
      },
      showRadio: {
        type: Boolean,
        default: false
      },
      showNumBox: {
        type: Boolean,
        default: false
      }
    },
    data() {
      return {
        // 商品图片地址为空时，显示的默认图片
        defaultPic: 'https://img3.doubanio.com/f/movie/8dd0c794499fe925ae2ae89ee30cd225750457b4/pics/movie/celebrity-default-medium.png'
      };
    },
    methods: {
      // 在购物车中商品左侧选择框点击处理函数
      radioClick(goods) {
        // 调用父组件cart.vue的更改商品是否选中状态的函数 radioChange
        this.$emit('radioChange', {
          goods_id: goods.goods_id,
          goods_state: !goods.goods_state  // 点击选择框后，要更改商品的选中状态为取反值
        })
      },
      // 商品数量框数值改变事件 change
      numChangeHandler(val){
        this.$emit('num-change',{
          goods_id: this.goods.goods_id,
          goods_count: +val  // +val 或 val - 0 都可以隐式转换为数值类型
        })
      }
    }
  }
</script>

<style lang="scss">
.goods-item {
  display: flex;
  // justify-content: space-between;
  width: 750rpx;
  box-sizing: border-box;
  padding: 5px;
  border-bottom: 1px solid #efefef;
  .goods-item-left {
    display: flex;
    justify-content: space-between;
    align-items: center;
    .goods-pic {
      width: 100px;
      height: 100px;
      display: block;  /* 防止图片底部有空白 */
    }
  }
  .goods-item-right {
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    width: 100%;
    padding: 0 15px 5px 5px;
    .goods-name {
      font-size: 13px;
      /* 最多显示3行，超过在3行后显示省略号 -start */
      display: -webkit-box;
      -webkit-line-clamp: 2;
      line-clamp: 2;
      -webkit-box-orient: vertical;
      box-orient: vertical;
      overflow: hidden;
      text-overflow: ellipsis;
      /* 最多显示3行，超过在3行后显示省略号 -end */
    }
    .goods-info-box {
      display: flex;
      justify-content: space-between;
      align-items: center;
      flex-grow: 1;
      .goods-price {
        color: #C00000;
      }
    }
  }
  
}
</style>
